package infore.com.cn.util;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class PooledDBA {
	private static PooledDBA instance;
	private static ComboPooledDataSource dataSource;

	private PooledDBA() throws SQLException, PropertyVetoException {
		dataSource = new ComboPooledDataSource();
		dataSource.setUser(ConfigUtil.getValue("user"));
		dataSource.setPassword(ConfigUtil.getValue("password"));
		dataSource.setJdbcUrl(ConfigUtil.getValue("url"));
		dataSource.setDriverClass(ConfigUtil.getValue("driver"));
		dataSource.setInitialPoolSize(10);
		dataSource.setMinPoolSize(1);
		dataSource.setMaxPoolSize(500);
		dataSource.setMaxStatements(800);
		dataSource.setMaxIdleTime(60);
	}

	public static final PooledDBA getInstance() {
		if (instance == null) {
			try {
				instance = new PooledDBA();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return instance;
	}

	public synchronized final Connection getConnection() {
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

}
